import java.util.Random;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 阿宾
 * Date: 2024-09-21
 * Time: 20:22
 */
public class StringBuilderDemo1 {
    public static void main(String[] args) {
        String str = "abcdefg";
        char[] arr = str.toCharArray();
        Random random = new Random();

        for (int i = 0; i < arr.length; i++) {
            int index = random.nextInt(arr.length);//随机生成一个下标，用于打乱字符数组
            char tmp = arr[i];
            arr[i] = arr[index];
            arr[index] = tmp;
        }
        String ret = new String(arr);
        System.out.println(ret);
    }
    public static void main6(String[] args) {
        //1.定义两个字符串
        String strA = "abcde";
        String strB = "CDeab";

        //2.调用方法进行比较
        boolean ret = check(strA,strB);

        //3.输出
        System.out.println(ret);
    }
    //检查字符串旋转后是否相同
    public static Boolean check(String str1,String str2){
        // "abcde"
        //第一次旋转"bcdea",第二次旋转"cdeab",第三次旋转"deabc",第四次旋转"eabcd",第五次旋转"abcde"
        //最多旋转字符长度次
        for (int i = 0; i < str1.length(); i++) {
            str1 = rotate(str1);
            if(str1.equals(str2)){
                return true;
            }
        }
        return false;
    }

    //向左旋转字符串
    public static String rotate(String str){
        //第一个字符
        char first = str.charAt(0);
        //剩余字符串
        String end = str.substring(1);
        //字符串拼接
        return end + first;
    }
    public static void main5(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String str;//123456a
        while (true) {
            System.out.println("请输入一个字符串：");

            str = scanner.next();

            //2.校验字符串
            boolean ret = checkStr(str);
            if(ret){
                break;
            }else {
                System.out.println("字符串不符合规则，重新输入");
                continue;
            }
        }

        //将内容变为罗马数字
        StringBuilder s = new StringBuilder();
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            int number = c - '0';
            String s1 = changeLuoMa(number);
            s.append(s1);
        }
        System.out.println(s);


    }
    public static String changeLuoMa(int number) {
        //定义数组，让索引和罗马数字对应
        String[] arr = {"", "Ⅰ","Ⅱ","Ⅲ","Ⅳ","Ⅴ","Ⅵ","Ⅶ","Ⅷ","Ⅸ"};
        return arr[number];
    }
    public static boolean checkStr(String str){
        //检验长度
        if(str.length() > 9){
            return false;
        }
        //检验数字
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            if(c < '0' || c > '9'){
                return false;
            }
        }
        return true;
    }
    public static void main4(String[] args) {
        String s1 = "abc";
        String s2 = "a" + "b" + "c";
        System.out.println(s1 == s2);
    }
    public static void main3(String[] args) {
        String s1 = "a";
        String s2 = s1 + "b";
        String s3 = s2 + "c";
        System.out.println(s3);

        //底层实现
        new StringBuilder().append(s1).append("b").toString();
        StringBuilder tmp = new StringBuilder();
        tmp.append(s1);
        tmp.append("b");
        tmp.append("c");
        String str = tmp.toString();
        System.out.println(str);
    }
    public static void main2(String[] args) {
        int[] arr = {1,2,3};
        String str = fun(arr);
        System.out.println(str);
    }
    public static String fun(int[] arr){
        StringBuilder sb = new StringBuilder("[");
        for (int i = 0; i < arr.length; i++) {
            if(i == arr.length - 1){
                sb.append(arr[i]).append("]");
            }else {
                sb.append(arr[i]).append(", ");
            }

        }
        return sb.toString();
    }
    public static void main1(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入一个字符串");
        String str = scanner.next();

        StringBuilder tmp = new StringBuilder();
        for (int i = str.length()-1; i >= 0; i--) {
            tmp.append(str.charAt(i));
        }
        String str1 = tmp.toString();
        if(str.equals(str1)){
            System.out.println("是对称字符串");
        }else {
            System.out.println("不是对称字符串");
        }
    }
}
